package cn.wolfcode.rbac.controller;
import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.qo.RoleQo;
import cn.wolfcode.rbac.service.PermissionService;
import cn.wolfcode.rbac.service.RoleService;
import cn.wolfcode.rbac.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author xiahuihui
 * #Description RoleController
 * #Date: 2022/7/2 13:52
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    //查询角色列表
    @RequestMapping("/list")
    @RequiredPermission({"角色列表","permission:list"})
    public String findRoleALl(Model model,@ModelAttribute("qo") RoleQo qo){
        //List<Role> roleAll = roleService.findRoleAll();
        //分页查询
        PageInfo<Role> pageResult = roleService.findByPage(qo);
        model.addAttribute("pageResult",pageResult);
        return "role/list";
    }

    //编辑角色
    @RequestMapping("/input")
    @RequiredPermission({"角色编辑","permission:input"})
    public String addRole(Model model,Long id){
        if(id != null){
            //根据id查询到对应的员工信息
            Role role = roleService.findById(id);
            //将员工信息带到页面，进行回显
            model.addAttribute("role",role);
        }
        //查询所有权限
        List<Permission> permissions = permissionService.findPermissionAll();
        model.addAttribute("permissions",permissions);
        return "role/input";
    }

    //插入角色
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission({"角色保存或更新","permission:saveOrUpdate"})
    public String saveRole(Role role,Long[] permissionIds){
        //保存角色
        roleService.save(role,permissionIds);
        return "redirect:list";
    }

    //删除角色
    @RequestMapping("/delete")
    @RequiredPermission({"角色删除","permission:delete"})
    public String deleteRole(Role role){
        roleService.delete(role);
        return "redirect:list";
    }
}